/**
 * @author Mie Plougstrup, Anders Abildgaard, Bo Stokholm 
 * @Version 16-12-2014
 */
package modelLayer;

import java.util.HashMap;

public class Disposition {
	private int dispId;
	private int chartecNo;
	private Driver driver;
	private Trailer trailer;
	private ChartecCode chartecCode;
	private String deliveryNo;
	private HashMap<Integer, Booking> bookings;


	/**
	 * Constructor 1: For creation from ctrClass.
	 * 
	 * @param int chartecNo
	 * @param Driver driver
	 * @param Trailer trailer
	 * @param ChartecCode chartecCode
	 * @param String deliveryNo
	 */
	public Disposition(int chartecNo, Driver driver, Trailer trailer, ChartecCode chartecCode, String deliveryNo) {
		this.dispId = -1;
		this.chartecNo = chartecNo;
		this.driver = driver;
		this.trailer = trailer;
		this.chartecCode = chartecCode;
		this.deliveryNo = deliveryNo;
		this.bookings = new HashMap<Integer, Booking>();
	}

	/**
	 * Constructor 2: For creation from dbClass.
	 */
	public Disposition() {
		this.dispId = -1;
		this.chartecNo = -1;
		this.driver = null;
		this.trailer = null;
		this.chartecCode = null;
		this.deliveryNo = null;
		this.bookings = new HashMap<Integer, Booking>();
	}

	/**
	 * Constructor 3: For creation as association.
	 * 
	 * @param int dispId
	 */
	public Disposition(int dispId) {
		this.dispId = dispId;
		this.chartecNo = -1;
		this.driver = null;
		this.trailer = null;
		this.chartecCode = null;
		this.deliveryNo = null;
		this.bookings = new HashMap<Integer, Booking>();
	}

	/**
	 * Add a booking to a disposition.
	 * 
	 * @param Booking b		The booking object.
	 * @return 				The key in the list (sequenceNo).
	 */
	public int addBooking(Booking b) {
		int key = bookings.size()+1;
		bookings.put(key, b);
		return key;
	}

	/**
	 * Remove a booking from a disposition.
	 * 
	 * @param sequenceNo	The sequenceNo of the booking to be removed.
	 */
	public void removeBooking(int sequenceNo) {
		int nextKey = sequenceNo + 1;
		int lastKey = bookings.size();
		for (int key = nextKey; key <= bookings.size(); key++) {
			int rowAboveKey = key - 1;
			bookings.put(rowAboveKey, bookings.get(key));
		}
		bookings.remove(lastKey);
	}

	/**
	 * Method for refactoring the booking sequence. Will move the specified booking 1 key up in the hashMap.
	 * 
	 * @param sequenceNo	The sequenceNo of the booking to be removed.
	 */
	public void refactorSequenceUp(int sequenceNo) {
		if (sequenceNo > 1) {
			Booking temp = bookings.get(sequenceNo-1);
			bookings.put(sequenceNo-1, bookings.get(sequenceNo));
			bookings.put(sequenceNo, temp);
		}

	}

	/**
	 * Method for refactoring the booking sequence. Will move the specified booking 1 key down in the hashMap.
	 * 
	 * @param sequenceNo	The sequenceNo of the booking to be removed.
	 */
	public void refactorSequenceDown(int sequenceNo) {
		if (sequenceNo < bookings.size()) {
			Booking temp = bookings.get(sequenceNo+1);
			bookings.put(sequenceNo+1, bookings.get(sequenceNo));
			bookings.put(sequenceNo, temp);
		}
	}

	/**
	 * Calculate total weight
	 * @return double totalWeight
	 */
	public double calculateTotalWeight() {
		double totalWeight = 0;

		if (bookings.size() != 0) {
			for(int key = 1; key <= bookings.size(); key++) {
				Booking b = bookings.get(key);
				totalWeight += b.getWeight(); 
			}
		}
		return totalWeight;
	}

	/**
	 * Calculate Total LDM
	 * @return double totalLDM
	 */
	public double calculateTotalLDM() {
		double totalLDM = 0;

		if (bookings.size() > 0) {
			for(int key = 1; key <= bookings.size(); key++) {
				Booking b = bookings.get(key);
				totalLDM += b.calculateLDM();
			}
		}
		return totalLDM;
	}

	/**
	 * Calculate total m3
	 * @return double totalM3
	 */
	public double calculateTotalM3() {
		double totalM3 = 0;

		for(int key = 1; key <= bookings.size(); key++) {
			Booking b = bookings.get(key);
			totalM3 += b.calculateM3(); 
		}
		return totalM3;
	}

	/**
	 * Get disposition id
	 * @return the dispId
	 */
	public int getDispId() {
		return dispId;
	}

	/**
	 * Set dispostion id
	 * @param dispId the dispId to set
	 */
	public void setDispId(int dispId) {
		this.dispId = dispId;
	}

	/**
	 * Get chartec no.
	 * @return the chartecNo
	 */
	public int getChartecNo() {
		return chartecNo;
	}

	/**
	 * Set chartec no.
	 * @param chartecNo the chartecNo to set
	 */
	public void setChartecNo(int chartecNo) {
		this.chartecNo = chartecNo;
	}

	/**
	 * Get chartec no.
	 * @return the driver
	 */
	public Driver getDriver() {
		return driver;
	}

	/**
	 * Set driver
	 * @param driver the driver to set
	 */
	public void setDriver(Driver driver) {
		this.driver = driver;
	}

	/**
	 * Get trailer
	 * @return the trailer
	 */
	public Trailer getTrailer() {
		return trailer;
	}

	/**
	 * Set trailer
	 * @param trailer the trailer to set
	 */
	public void setTrailer(Trailer trailer) {
		this.trailer = trailer;
	}

	/**
	 * Get chartec code
	 * @return the chartecCode
	 */
	public ChartecCode getChartecCode() {
		return chartecCode;
	}

	/**
	 * Set chartec code
	 * @param chartecCode the chartecCode to set
	 */
	public void setChartecCode(ChartecCode chartecCode) {
		this.chartecCode = chartecCode;
	}

	/**
	 * Get delivery no.
	 * @return the deliveryNo
	 */
	public String getDeliveryNo() {
		return deliveryNo;
	}

	/**
	 * Set delivery no.
	 * @param deliveryNo the deliveryNo to set
	 */
	public void setDeliveryNo(String deliveryNo) {
		this.deliveryNo = deliveryNo;
	}

	/**
	 * Get the bookings
	 * @return the bookings
	 */
	public HashMap<Integer, Booking> getBookings() {
		return bookings;
	}

	/**
	 * Set booking
	 * @param bookings the bookings to set
	 */
	public void setBookings(HashMap<Integer, Booking> bookings) {
		this.bookings = bookings;
	}

}
